﻿using Learun.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using static Learun.Application.TwoDevelopment.ZZDT_EC.ProjectSugar;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-08-23 17:31
    /// 描 述：命名规则，与对象类型和属性都有关联
    /// </summary>
    public class ec_objecttypenamingconvBLL
    {
        private ec_objecttypenamingconvService ec_objecttypenamingconvService = new ec_objecttypenamingconvService();

        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_objecttypenamingconvEntity> GetList(string queryJson, Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                var ProjectId = queryParam["ProjectId"]?.ToString();
                var ObjectTypeID = queryParam["ObjectTypeID"]?.ToString();

                var tableName = TableName<ec_objecttypenamingconvEntity>(ProjectId);
                var query = Db.Queryable<ec_objecttypenamingconvEntity>().AS(tableName)
                    .WhereIF(!string.IsNullOrEmpty(ObjectTypeID), x => x.ObjectTypeID == ObjectTypeID)
                    .OrderByDescending(x => x.Segmentno);
                List<ec_objecttypenamingconvEntity> list;
                if (pagination == null)
                {
                    list = query.ToList();
                }
                else
                {
                    list = query.ToPageList(pagination.page, pagination.rows);
                }
                var listProp = new ec_objecttypepService().GetObjectTypePById(ObjectTypeID, ProjectId, true).ToList();
                list.ForEach(x =>
                {
                    var res2 = listProp.First(y => y.PropertyID == x.ObjectTypePID);
                    if (res2 != null)
                    {
                        x.DecimalPoint = res2.DecimalPoint;
                        x.DefaultUnitName = res2.DefaultUnitName;
                        x.DefaultUnit = res2.DefaultUnit;
                        x.EnumData = res2.EnumData;
                        x.EnumDataCode = res2.EnumDataCode;
                        x.MeasuringUnitTypeID = res2.MeasuringUnitTypeID;
                        x.MeasuringUnitTypeName = res2.MeasuringUnitTypeName;
                        x.PropertyGID = res2.PropertyGID;
                        x.PropertyGroupName = res2.PropertyGroupName;
                        x.PropertyName = res2.PropertyName;
                        x.ObjectTypePName = res2.PropertyName;//web端显示的是这个属性
                        x.PropertyNameEN = res2.PropertyNameEN;
                        x.PropertyType = res2.PropertyType;
                    }
                });
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_objecttypenamingconvEntity GetEntity(string keyValue, string ProjectId)
        {
            try
            {
                return ec_objecttypenamingconvService.GetEntity(keyValue, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue, string ProjectId)
        {
            try
            {
                ec_objecttypenamingconvService.DeleteEntity(keyValue,ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, ec_objecttypenamingconvEntity entity, string ProjectId)
        {
            try
            {
                ec_objecttypenamingconvService.SaveEntity(keyValue, entity, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

    }
}
